package com.onefish.toolkit.jvm;

import cn.hutool.core.io.FileUtil;
import org.springframework.web.util.HtmlUtils;

import java.io.File;
import java.lang.management.ManagementFactory;
import java.util.ArrayList;
import java.util.List;

public class HeapMemoryOverflow {
//    private static final List<char[]> list = new ArrayList<>();
    public static void main(String[] args) throws Exception {
        // 设置堆内存大小为10MB
        System.out.println("Heap size: " + (Runtime.getRuntime().maxMemory() / 1024 / 1024) + "MB");
        String name = ManagementFactory.getRuntimeMXBean().getName();
        String pid = name.split("@")[0];
        System.out.println("Current process ID: " + pid);
        Thread.sleep(60000);
        // 尝试创建大量char数组，直到内存溢出
        while (true) {
            char[] array = new char[1024 * 1024]; // 1MB
//            list.add(array);
            System.out.println("Allocated " + (2 * array.length / 1024 / 1024) + " MB");
        }
    }
}
